package model.workersWithData;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

import entities.IGarage;
import entities.IMaster;

public class WorkerWithGarage {

	private static final String ALL_GARAGES_BUSY = "All garages busy";
	private static final String EMPTY_GARAGE = "Empty garage #";
	private static final String PLACE_ON_THIS_DATE = " empty places on date";
	private static final String GARAGE_EXISTS = "Sorry, garage with this number already exists";
	private static final String GARAGE_ADD = "Thanks, garage was add";
	private static final String GARAGE_DELETE = "This garage was deleted";
	private static final String GARAGE_NOT_EXISTS = "Sorry, garage with this nhumber is absent";
	private static final Logger log = Logger.getLogger(WorkerWithGarage.class);

	public static void getEmptyGarage(ArrayList<IGarage> garageList) {
		try {
			boolean temp = false;
			for (IGarage garage : garageList) {
				if (garage.getEmptyGarage()) {
					System.out.println(EMPTY_GARAGE + garage.getNumberGarage());
					temp = true;
				}
			}

			if (temp == false) {
				System.out.println(ALL_GARAGES_BUSY);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	public static void getNumberEmptyPlaces(ArrayList<IMaster> masterList,
			ArrayList<IGarage> garageList, GregorianCalendar date) {
		try {
			ArrayList<IGarage> garageEmpty = new ArrayList<IGarage>();
			ArrayList<IMaster> masterFree = new ArrayList<IMaster>();

			for (IGarage garage : garageList) {
				if (garage.getOrder().getDeadlineDate().before(date)
						|| garage.getEmptyGarage()) {
					garageEmpty.add(garage);
				}
			}

			for (IMaster master : masterList) {
				if (master.getOrder().getDeadlineDate().before(date)
						|| master.getEmploymentMaster() == false) {
					masterFree.add(master);
				}
			}

			if (masterFree.size() > garageEmpty.size()) {
				System.out.println(garageEmpty.size() + PLACE_ON_THIS_DATE);
			} else if (masterFree.size() < garageEmpty.size()) {
				System.out.println(masterFree.size() + PLACE_ON_THIS_DATE);
			} else if (masterFree.size() == garageEmpty.size()) {
				System.out.println(garageEmpty.size() + PLACE_ON_THIS_DATE);
			}

		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	public static void addGarage(ArrayList<IGarage> garageList, IGarage garage) {
		try {
			int i = 0;
			for (IGarage g : garageList) {
				if (g.getNumberGarage() == garage.getNumberGarage()) {
					System.out.println(GARAGE_EXISTS);
					i++;
				}
			}
			if (i == 0) {
				garageList.add(garage);
				System.out.println(GARAGE_ADD);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	public static void deleteGarage(ArrayList<IGarage> garageList,
			Integer numberGarage) {
		try {
			ArrayList<IGarage> temp = new ArrayList<IGarage>();
			for (IGarage gar : garageList) {
				if (gar.getNumberGarage() != numberGarage) {
					temp.add(gar);

				}
			}

			if (temp.size() == garageList.size()) {
				System.out.println(GARAGE_NOT_EXISTS);
			} else {
				System.out.println(GARAGE_DELETE);
				garageList.clear();
				garageList.addAll(temp);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}

	}

}
